<?php

namespace App\Jobs\QiPao\Gift;

use App\Jobs\Job;
use App\Services\Tools\IdempotentConsume;
use Illuminate\Support\Facades\Log;

class GiftWallRankJob extends Job
{
    protected $data;
    const jobName = 'GiftWallRankJob';

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(env('APP_ENV') != 'production'){
            Log::error('giftWallRankJob:' . json_encode(['time' => getMillisecond(), 'data' => $this->data]));
        }
        try {
            //todo 未更新礼物墙评分
            //更新礼物墙排行榜
            $idempotentConsumeServer =  new IdempotentConsume();
            $data = $this->data;
            $uk = $data['id']??'';
            if($idempotentConsumeServer->checkIdempotentRedisKey(self::jobName,$uk)){
                $lastNumber = app('model_user_gift_wall')->where('user_id', $this->data['gift_user'])->where('gift_id', $this->data['gift_id'])->value('number');
                app('repo_gift_wall')->updateUserGiftWallRank($this->data['gift_user'], $this->data['gift_id'], $lastNumber ? $lastNumber : 0);
            }
        } catch (\Exception $exception) {
            $idempotentConsumeServer->deleteIdempotentRedisKey(self::jobName,$uk);
            Log::error('GiftWallRankJob:'.$exception->getMessage());
        }
    }
    
    /*public function fail($exception = null )
    {
        Log::error('GiftWallRankJob:'.$exception->getMessage());
    }*/
}
